package top.csmcool.musicapi.client;

/*
 * @description
 * @date 2025/1/31
 * @author shijin
 * @version 1.0
 */

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import top.csmcool.musicapi.enums.PlatformEnum;
import top.csmcool.musicapi.model.api.kugou.MusicInfo;
import top.csmcool.musicapi.model.api.netease.*;
import top.csmcool.musicapi.util.MusicRequestUtil;

import java.util.LinkedList;
import java.util.List;

@Component
@RequiredArgsConstructor
public class NeteaseClient extends CommonClient{

    private final MusicRequestUtil musicRequestUtil;

    public List<MusicTrack> playlistDetail(JSONObject parameter) {
        parameter.set(getMETHOD_NAME(), "playlistDetail");
        JSONObject jsonObject = musicRequestUtil.sendRequestAutowiredJson(parameter, getUrl(parameter));
        if(jsonObject == null
                || jsonObject.getJSONObject("playlist") == null
                || jsonObject.getJSONObject("playlist").getJSONArray("tracks") == null) {
            return null;
        }

        JSONArray jsonArray = jsonObject.getJSONObject("playlist").getJSONArray("tracks");
        return JSONUtil.toList(jsonArray, MusicTrack.class);
    }

    public String lyric(JSONObject parameter) {
        parameter.set(getMETHOD_NAME(), "lyric");
        JSONObject jsonObject = musicRequestUtil.sendRequestAutowiredJson(parameter, getUrl(parameter));
        if(jsonObject == null
                || jsonObject.getJSONObject("lrc") == null
                || jsonObject.getJSONObject("lrc").getStr("lyric") == null) {
            return null;
        }

        JSONObject lrc = jsonObject.getJSONObject("lrc");
        return lrc.getStr("lyric");
    }

    public List<MusicPlayList> userPlaylist(JSONObject parameter) {
        parameter.set(getMETHOD_NAME(), "userPlaylist");
        JSONObject jsonObject = musicRequestUtil.sendRequestAutowiredJson(parameter, getUrl(parameter));
        if(jsonObject == null
                || jsonObject.get("playlist") == null) {
            return new LinkedList<>();
        }

        JSONArray playlist = jsonObject.getJSONArray("playlist");
        return JSONUtil.toList(playlist, MusicPlayList.class);
    }

    public List<Playlist> personalized(JSONObject parameter) {
        parameter.set(getMETHOD_NAME(), "personalized");
        JSONObject jsonObject = musicRequestUtil.sendRequestAutowiredJson(parameter, getUrl(parameter));
        if(jsonObject == null
                || jsonObject.get("result") == null) {
            return new LinkedList<>();
        }
        JSONArray playlist = jsonObject.getJSONArray("result");
        return JSONUtil.toList(playlist, Playlist.class);
    }

    public List<Song> search(JSONObject parameter) {
        parameter.set(getMETHOD_NAME(), "search");
        JSONObject jsonObject = musicRequestUtil.sendRequestAutowiredJson(parameter, getUrl(parameter));
        if(jsonObject == null
                || jsonObject.getJSONObject("result") == null
                || jsonObject.getJSONObject("result").getJSONArray("songs") == null) {
            return new LinkedList<>();
        }
        JSONArray jsonArray = jsonObject.getJSONObject("result").getJSONArray("songs");
        return JSONUtil.toList(jsonArray, Song.class);
    }

    public List<SongData> songUrl(JSONObject parameter) {
        parameter.set(getMETHOD_NAME(),"songUrl");
        JSONObject entries = musicRequestUtil.sendRequestAutowiredJson(parameter, getUrl(parameter));
        if(entries == null
                || entries.isEmpty()
                || entries.get("data") == null) {
            return null;
        }
        JSONArray data = entries.getJSONArray("data");
        return JSONUtil.toList(data, SongData.class);
    }

    @Override
    public PlatformEnum getPlatform() {
        return PlatformEnum.NETEASE;
    }

    public JSONObject requestResource(JSONObject jsonObject) {
        return musicRequestUtil.sendRequestAutowiredJson(jsonObject, getUrl(jsonObject));
    }
}
